"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _system = require("../../../../static/utils/system.js");

var _system2 = _interopRequireDefault(_system);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {
    default: obj
  };
}

var app = getApp();
exports.default = Page({
  data: {
    isIntegral: false,
    isGift: false
  },
  onLoad: function onLoad(e) {
    var type = e.type;
    var _this = this;
    var host = app.globalData.host;
    var openid = _this.data.openid || wx.getStorageSync("openid") || "";
    var data = {
      mod: "category",
      act: "list",
      ssid: openid,
      mid: "xydj"
    };
    wx.setNavigationBarTitle({
      title: type
    });
    _this.setData({
      info: wx.getStorageSync("info")
    });
    wx.request({
      url: host,
      data: data,
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function success(res) {
        _this.setData({
          serviceType: type == "升级" ? "upgrade" : "renew",
          introduce: res.data.tab,
          proList: res.data.info,
          openid: openid
        });
      }
    });
  },
  integral: function integral() {
    var _this = this;
    var status = _this.data.isIntegral;
    if (status) {
      _this.setData({
        isIntegral: false
      });
    } else {
      _this.setData({
        isIntegral: true
      });
    }
  },
  gift: function gift() {
    var _this = this;
    var status = _this.data.isGift;
    if (status) {
      _this.setData({
        isGift: false
      });
    } else {
      _this.setData({
        isGift: true
      });
    }
  },
  selectType: function selectType(e) {
    var _this = this;
    var mid = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    _this.setData({
      mid: mid,
      type: type
    });
  },
  pay: function pay(e) {
    var _this = this;
    var openid = _this.data.openid;
    var opt = _this.data.serviceType;
    var mid = _this.data.mid;
    var type = _this.data.type;
    var isGift = _this.data.isGift;
    var mobile = e.detail.value.mobile || "";
    var payway = _this.data.isIntegral == true ? "integral" : "cash";
    var memo = "";
    wx.request({
      url: app.globalData.host,
      data: {
        mod: "do",
        act: "select",
        ssid: openid,
        opt: opt,
        mid: mid,
        mobile: mobile,
        payway: payway,
        memo: memo
      },
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function success(res) {
        if (isGift) {
          if (!mobile) {
            wx.showToast({
              title: "请输入手机号码！",
              icon: "none",
              duration: 2000,
              mask: false
            });
          } else {
            var ret = res.data.ret;
            if (payway == "cash") {
              if (type) {
                if (ret != -1) {
                  var oid = res.data.info.oid;
                  var amount = res.data.info.amount;
                  var _openid = _this.data.openid;
                  wx.request({
                    url: app.globalData.WxHost + "ssid=" + _openid + "&mid=" + oid + "&money=" + amount,
                    success: function success(response) {
                      wx.requestPayment({
                        appId: response.data.appId,
                        nonceStr: response.data.nonceStr,
                        timeStamp: response.data.timeStamp,
                        package: response.data.package,
                        signType: response.data.signType,
                        paySign: response.data.paySign,
                        success: function success(result) {
                          _this.setData({
                            mid: "",
                            mobile: "",
                            payway: "",
                            isIntegral: false,
                            isGift: false,
                            type: null
                          }, function () {
                            wx.showToast({
                              title: "购买成功！",
                              icon: "none",
                              duration: 2000,
                              mask: false,
                              success: function success() {
                                setTimeout(function () {
                                  wx.request({
                                    url: app.globalData.host,
                                    data: {
                                      mod: "category",
                                      act: "list",
                                      ssid: _this.data.openid,
                                      mid: "xydj"
                                    },
                                    method: "post",
                                    header: {
                                      "Content-Type": "application/x-www-form-urlencoded"
                                    },
                                    success: function success(res) {
                                      var data = {
                                        mod: "do",
                                        act: "info",
                                        ssid: _openid
                                      };
                                      _system2.default.getUserInfo(data, "post");
                                      setTimeout(function () {
                                        _this.setData({
                                          info: wx.getStorageSync("info")
                                        });
                                      }, 1000);
                                    }
                                  });
                                }, 1000);
                              }
                            });
                          });
                        },
                        fail: function fail(result) {
                          wx.showToast({
                            title: "支付失败！",
                            icon: "none",
                            duration: 2000,
                            mask: false
                          });
                        }
                      });
                    }
                  });
                } else {
                  wx.showToast({
                    title: res.data.txt,
                    icon: "none",
                    duration: 2000,
                    mask: false
                  });
                }
              } else {
                wx.showToast({
                  title: "请选择会员种类！",
                  icon: "none",
                  duration: 2000,
                  mask: false
                });
              }
            } else {
              wx.showToast({
                title: res.data.txt,
                icon: "none",
                duration: 2000,
                mask: false
              });
            }
          }
        } else {
          var _ret = res.data.ret;
          if (payway == "cash") {
            if (type) {
              if (_ret != -1) {
                var _oid = res.data.info.oid;
                var _amount = res.data.info.amount;
                var _openid2 = _this.data.openid;
                wx.request({
                  url: app.globalData.WxHost + "ssid=" + _openid2 + "&mid=" + _oid + "&money=" + _amount,
                  success: function success(response) {
                    wx.requestPayment({
                      appId: response.data.appId,
                      nonceStr: response.data.nonceStr,
                      timeStamp: response.data.timeStamp,
                      package: response.data.package,
                      signType: response.data.signType,
                      paySign: response.data.paySign,
                      success: function success(result) {
                        _this.setData({
                          mid: "",
                          mobile: "",
                          payway: "",
                          isIntegral: false,
                          isGift: false,
                          type: null
                        }, function () {
                          wx.showToast({
                            title: "购买成功！",
                            icon: "none",
                            duration: 2000,
                            mask: false,
                            success: function success() {
                              setTimeout(function () {
                                wx.request({
                                  url: app.globalData.host,
                                  data: {
                                    mod: "category",
                                    act: "list",
                                    ssid: _this.data.openid,
                                    mid: "xydj"
                                  },
                                  method: "post",
                                  header: {
                                    "Content-Type": "application/x-www-form-urlencoded"
                                  },
                                  success: function success(res) {
                                    var data = {
                                      mod: "do",
                                      act: "info",
                                      ssid: _openid2
                                    };
                                    _system2.default.getUserInfo(data, "post");
                                    setTimeout(function () {
                                      _this.setData({
                                        info: wx.getStorageSync("info")
                                      });
                                    }, 1000);
                                  }
                                });
                              }, 1000);
                            }
                          });
                        });
                      },
                      fail: function fail(result) {
                        wx.showToast({
                          title: "支付失败！",
                          icon: "none",
                          duration: 2000,
                          mask: false
                        });
                      }
                    });
                  }
                });
              } else {
                wx.showToast({
                  title: res.data.txt,
                  icon: "none",
                  duration: 2000,
                  mask: false
                });
              }
            } else {
              wx.showToast({
                title: "请选择会员种类！",
                icon: "none",
                duration: 2000,
                mask: false
              });
            }
          } else {
            wx.showToast({
              title: res.data.txt,
              icon: "none",
              duration: 2000,
              mask: false
            });
          }
        }
      }
    });
  }
});